﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD;
using Microsoft.VisualBasic;
using System.Windows.Forms;
using Autodesk.AutoCAD.Interop.Common;
using System.IO;
//using Autodesk.AutoCAD.Windows;
using MyIni;
namespace MyCadDll
{
    public class MyCadDll
    {
        Autodesk.AutoCAD.Interop.AcadApplication AcadApp;
        Autodesk.AutoCAD.Interop.AcadDocument AcadDoc;
        AcadSelectionSet mySelectionSet;
        IniFile myini=new IniFile(Directory.GetCurrentDirectory()+"\\mycad.ini");
        int intset;
        //启动CAD文件
        public void StartCad()
        {
            try
            {
                AcadApp = (AcadApplication)System.Runtime.InteropServices.Marshal.GetActiveObject("AutoCAD.Application");
                AcadDoc = AcadApp.ActiveDocument;
            }
            catch
            {
                OpenFileDialog POpenDG = new OpenFileDialog();
                POpenDG.ShowDialog();
                string filePath = POpenDG.FileName;
                AcadApp = new AcadApplication();
                AcadDoc = AcadApp.Documents.Open(filePath, null, null);
            }
            AcadApp.Application.Visible = true;
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
        }
        //返回CAD选择集到数组
        public string[] GetSelectSet()
        {
            intset++;
            string NewSelectionSet003 = intset.ToString();
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            // AcadSelectionSet mySelectionSet;
            mySelectionSet = AcadDoc.SelectionSets.Add(NewSelectionSet003);
            Int16[] FilterType = new Int16[1];
            object[] FilterData = new object[1];
            FilterType[0] = 0;
            FilterData[0] = "*";
            mySelectionSet.SelectOnScreen(FilterType, FilterData);
            int z = mySelectionSet.Count;
            String[] textArray=new String[z];
            int count = 0;
           // string showInforText = "";
           // int tempsum = myini.GetInt16("jy", "sum", 0);
            foreach (AcadObject acadObj in mySelectionSet)
            {
                if (acadObj.ObjectName == "AcDbText" || acadObj.ObjectName == "AcDbMText")
                {
                    //count++;
                    //textArray = new String[count];
                    //textArray[count - 1] = ((AcadText)acadObj).TextString;
                    //showInforText = textArray[count - 1].ToString();
                    //myini.WriteValue("jy", (++tempsum).ToString(), showInforText);
                    //myini.WriteValue("jy", (++tempsum).ToString(), ((AcadText)acadObj).TextString);
                    textArray[count++]=((AcadText)acadObj).TextString;
                }
               // myini.WriteValue("jy", "sum", tempsum);
            }

            // textBox1.Text = showInforText;
            AcadDoc.SelectionSets.Item(NewSelectionSet003).Delete();
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            return textArray;
        }
        //读选择集到mycad.ini的‘cad数据’节中
        public void ReadSelectSetToIni()
        {
            intset++;
            string NewSelectionSet003 = intset.ToString();
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            mySelectionSet = AcadDoc.SelectionSets.Add(NewSelectionSet003);
            Int16[] FilterType = new Int16[1];
            object[] FilterData = new object[1];
            FilterType[0] = 0;
            FilterData[0] = "*";
            mySelectionSet.SelectOnScreen(FilterType, FilterData);

           // String[] textArray;
           // int count = 0;
           // string showInforText = "";
            int tempsum = myini.GetInt16("CAD数据", "sum", 0);
            foreach (AcadObject acadObj in mySelectionSet)
            {
                if (acadObj.ObjectName == "AcDbText" || acadObj.ObjectName == "AcDbMText")
                {
                   
                    myini.WriteValue("CAD数据", (++tempsum).ToString(), ((AcadText)acadObj).TextString);
                }
                myini.WriteValue("CAD数据", "sum", tempsum);
            }

            AcadDoc.SelectionSets.Item(NewSelectionSet003).Delete();
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
        }
        //读选择集到mycad.ini的指定section节中
        public void ReadSelectSetToIni(string section)
        {
            intset++;
            string NewSelectionSet003 = intset.ToString();
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            mySelectionSet = AcadDoc.SelectionSets.Add(NewSelectionSet003);
            Int16[] FilterType = new Int16[1];
            object[] FilterData = new object[1];
            FilterType[0] = 0;
            FilterData[0] = "*";
            mySelectionSet.SelectOnScreen(FilterType, FilterData);

            // String[] textArray;
            // int count = 0;
            // string showInforText = "";
            int tempsum = myini.GetInt16(section, "sum", 0);
            foreach (AcadObject acadObj in mySelectionSet)
            {
                if (acadObj.ObjectName == "AcDbText" || acadObj.ObjectName == "AcDbMText")
                {

                    myini.WriteValue(section, (++tempsum).ToString(), ((AcadText)acadObj).TextString);
                }
                myini.WriteValue(section, "sum", tempsum);
            }

            AcadDoc.SelectionSets.Item(NewSelectionSet003).Delete();
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
        }
        public void WriteToCad(double[] p,String s,int height)
        {
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            AcadDoc.ModelSpace.AddText(s, p, height);
        }


       
    }
}
